/**
 * 元数据文件提供器实现
 */
package com.flute.framework.data.impl;

import java.io.File;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.List;

import com.flute.framework.FluteMonitor;
import com.flute.framework.data.IRawDataProvider;
import com.flute.framework.data.constants.Constants;
import com.flute.framework.monitor.EventLogger;
import com.flute.framework.util.EventLoggerUtil;
import com.flute.framework.util.FileToolsUtil;

/**
 * @author Jazz
 */
public final class RawDataFileProvider implements IRawDataProvider<File> {
    /**
     * 处理数据的地址
     */
    private String path = "e:/home/release/lily/processedData/album";

    /*
     * 返回所有原数据
     * 
     * @see com.flute.framework.data.IRawDataProvider#getRawDatas()
     */
    @Override
    public List<File> getRawDatas() {
        EventLogger.startNewEvent(FluteMonitor.getImporterKey());
        EventLoggerUtil.info("开始调用getRawDatas()方法......", FluteMonitor
                .getImporterKey());
        List<File> fileList = new ArrayList<File>();

        File dir = new File(getPath());

        File[] files = dir.listFiles();

        if (files != null) {
            for (File file : files) {
                String fileName = file.getName();
                if (!isTmpFile(fileName) && !file.isDirectory()
                        && !file.isHidden()
                        && FileToolsUtil.isValidXMLFile(fileName)) {
                    fileList.add(file);
                }
            }
        }

        EventLoggerUtil.info("调用getRawDatas()方法结束......", FluteMonitor
                .getImporterKey());
        return fileList;
    }

    public String getPath() {
        if (path == null || path.equals("")) {
            throw new InvalidParameterException("无效的path参数,请检查后再试");
        }
        return path;
    }

    public void setPath(final String p) {
        this.path = p;
    }

    private boolean isTmpFile(final String fileName) {
        return fileName.endsWith(Constants.TMP_SUFFIX);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        final RawDataFileProvider st = new RawDataFileProvider();
        // st.getRawDatas();
        Runnable runnable = new Runnable() {

            @Override
            public void run() {
                st.getRawDatas();
            }

        };
        for (int i = 0; i < 10; i++) {
            Thread t = new Thread(runnable, "" + i);
            t.start();
        }
    }
}
